package com.prs.crm.action.pm;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.OaTask;
import com.prs.crm.domain.oa.OaTaskSource;
import com.prs.crm.domain.oa.OaTaskType;
import com.prs.crm.domain.pm.PmGroupMember;
import com.prs.crm.domain.pm.PmProject;
import com.prs.crm.domain.pm.PmStage;
import com.prs.crm.service.oa.OaTaskService;
import com.prs.crm.service.oa.OaTaskSourceService;
import com.prs.crm.service.oa.OaTaskTypeService;
import com.prs.crm.service.pm.PmStageService;

public class PmStageTaskAction extends BaseAction{
	private PmStage pmStage;
	private OaTask oaTask;
	private PmStageService pmStageService;
	private OaTaskService oaTaskService;
	private OaTaskTypeService oaTaskTypeService;
	private OaTaskSourceService oaTaskSourceService;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	private File upload;
	private String uploadFileName;
	private String principals;
	private String principals_name;
	private String copyToPersons;
	private String copyToPerson_names;
	private TreeNode root;
	
	private List<OaTaskType> oaTaskTypes;
	private List<OaTaskSource> oaTaskSources;
	
	@Action("listPmStageTask")
	public String listPmStageTask() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PmStage.class);
		criteria.createAlias("pmGroup", "g");
		criteria.add(Restrictions.eq("g.principal", AppContext.getAccountContext().currentUser()));
		criteria.add(Restrictions.eq("state", Constrains.PM_STATE_START));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmStageService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editPmStageTask")
	public String editPmStageTask() {
		oaTaskTypes = this.getOaTaskTypeService().getAllTaskType();
		oaTaskSources = this.getOaTaskSourceService().getAllTaskSource();
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="saveTask",results={@Result(name="success",type="redirect",location="editTask")})
	public String save() {
		this.setPrompt("创建阶段任务成功");
		oaTask.setState(Constrains.TASK_EXECUTE);		
		this.getOaTaskService().saveTask(oaTask, upload, uploadFileName,principals,copyToPersons);
		return SUCCESS;
	}
	
	@Action(value="loadExecutorsTree",results={@Result(name="success",type="json",params={"root","root"})})
	public String loadExecutorsTree() {
		root = new TreeNode();
		root.setId("-1");
		
		TreeNode node = new TreeNode();
		node.setId("0");
		node.setText("选择负责人");	
		
		if(pmStage != null) {
			for(PmGroupMember member : pmStage.getPmGroup().getMembers()) {
				TreeNode m = new TreeNode();
				m.setId(member.getId().toString());
				m.setText(member.getMember().getResume().getName());
				
				node.addChildNode(m);
			}
		}
		
		root.addChildNode(node);
		return SUCCESS;
	}
	
	@Action(value="loadCritiquersTree",results={@Result(name="success",type="json",params={"root","root"})})
	public String loadGritiquersTree() {
		root = new TreeNode();
		root.setId("-1");
		
		TreeNode node = new TreeNode();
		node.setId("0");
		node.setText("选择点评人");	
		
		if(pmStage != null) {
			for(Person person : pmStage.getProject().getManageGroup()) {
				TreeNode m = new TreeNode();
				m.setId(person.getId().toString());
				m.setText(person.getResume().getName());
				
				node.addChildNode(m);
			}
		}
		
		root.addChildNode(node);
		return SUCCESS;
	}
	
	public String loadCritiqueMemberTree() {
		return SUCCESS;
	}
	
	public PmStage getPmStage() {
		return pmStage;
	}
	public void setPmStage(PmStage pmStage) {
		this.pmStage = pmStage;
	}
	public PmStageService getPmStageService() {
		return pmStageService;
	}
	public void setPmStageService(PmStageService pmStageService) {
		this.pmStageService = pmStageService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public OaTask getOaTask() {
		return oaTask;
	}

	public void setOaTask(OaTask oaTask) {
		this.oaTask = oaTask;
	}

	public OaTaskService getOaTaskService() {
		return oaTaskService;
	}

	public void setOaTaskService(OaTaskService oaTaskService) {
		this.oaTaskService = oaTaskService;
	}

	public OaTaskTypeService getOaTaskTypeService() {
		return oaTaskTypeService;
	}

	public void setOaTaskTypeService(OaTaskTypeService oaTaskTypeService) {
		this.oaTaskTypeService = oaTaskTypeService;
	}

	public OaTaskSourceService getOaTaskSourceService() {
		return oaTaskSourceService;
	}

	public void setOaTaskSourceService(OaTaskSourceService oaTaskSourceService) {
		this.oaTaskSourceService = oaTaskSourceService;
	}

	public List<OaTaskType> getOaTaskTypes() {
		return oaTaskTypes;
	}

	public void setOaTaskTypes(List<OaTaskType> oaTaskTypes) {
		this.oaTaskTypes = oaTaskTypes;
	}

	public List<OaTaskSource> getOaTaskSources() {
		return oaTaskSources;
	}

	public void setOaTaskSources(List<OaTaskSource> oaTaskSources) {
		this.oaTaskSources = oaTaskSources;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getPrincipals() {
		return principals;
	}

	public void setPrincipals(String principals) {
		this.principals = principals;
	}

	public String getCopyToPersons() {
		return copyToPersons;
	}

	public void setCopyToPersons(String copyToPersons) {
		this.copyToPersons = copyToPersons;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setPrincipals_name(String principals_name) {
		this.principals_name = principals_name;
	}

	public String getPrincipals_name() {
		return principals_name;
	}

	public String getCopyToPerson_names() {
		return copyToPerson_names;
	}

	public void setCopyToPerson_names(String copyToPersonNames) {
		copyToPerson_names = copyToPersonNames;
	}

}
